﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mail;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Contact_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }




    public static bool CheckEmail(string str)
    {
        Regex re = new Regex(@"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
        if (re.IsMatch(str))
            return true;
        else
            return false;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        
        try
        {
            if (
            CheckEmail(textEmail.Text))
            {
                SendMail();
                lblStatus.Text = "da~ send :v!!!";

            }
            else
            {
                lblStatus.Text = "Email kd";
            }
            
        }
        catch (Exception ex)
        {
            lblStatus.Text = ex.Message;
        }
        
 

          
    }


    protected void SendMail()
    {
        var fromAddress = "hellbaron93@gmail.com";
        // any address where the email will be sending
        var toAddress = "hellbaron93@gmail.com";
        //Password of your gmail address
        const string fromPassword = "kedaica123";
        // Passing the values and make a email formate to display
        string subject = textName.Text.ToString();
        string body = "From: " + textName.Text + "\n";
        body += "Email: " + textEmail.Text + "\n";
        body += "Subject: " + textName.Text + "\n";
        body += "Question: \n" + TextMessage.Text + "\n";
        // smtp settings
        var smtp = new System.Net.Mail.SmtpClient();
        {
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
            smtp.Timeout = 20000;
        }
        // Passing values to smtp object
        smtp.Send(fromAddress, toAddress, subject, body);

    }
}

